import matplotlib.pyplot as plt
import numpy as np

import pynumpak


def test():
    def f(x):
        return x ** 4 + x ** 3 + 7 * x ** 2 + 10 * x - 20

    ab = (-2, 2)
    x0 = np.linspace(-2, 2, 20)
    p1 = pynumpak.interpolate.lagrange_minres(f, ab)
    x1 = np.asarray([-1, 0, 1, 2])
    p2 = pynumpak.interpolate.discrete_poly_fit(x0, f(x0), 3)
    p3 = pynumpak.interpolate.continous_poly_fit(f, ab, 3)
    print(pynumpak.integrate.mse(f, p1, ab), pynumpak.integrate.mse(f, p2, ab), pynumpak.integrate.mse(f, p3, ab))

    plt.plot(x0, f(x0), x0, p1(x0), 'r', x0, p2(x0), 'g', x0, p3(x0), 'b')
    plt.show()


if __name__ == "__main__":
    test()
